﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolLibrary.Utils;
using SchoolLibrary.Entity;
using SchoolLibrary.DataBase.DAO;
using SchoolLibrary.Entity.Person;

namespace SchoolLibrary.Desktop.Guide.Input
{
    public partial class ClassInputDialog : Form
    {
        private Class pupilClass;

        public ClassInputDialog()
        {
            InitializeComponent();
            this.numberBox.TextChanged += new System.EventHandler(this.validate);
            this.literBox.TextChanged += new System.EventHandler(this.validate);
            initData();
        }

        public ClassInputDialog(Class pupilClass) : this()
        {
            this.pupilClass = pupilClass;
            this.literBox.Text = pupilClass.Letter;
            this.numberBox.Text = pupilClass.Number.ToString(); ;
        }

        private void initData()
        {
            List<ClassLeader> classLeaders = ClassLeaderDAO.getInstance().findAll();
            foreach (ClassLeader classLeader in classLeaders)
            {
                this.classLeaderComboBox.Items.Add(classLeader);
            }
            if (this.classLeaderComboBox.Items.Count > 0)
            {
                this.classLeaderComboBox.SelectedIndex = 0;
            }
        }

        private void setClassLeader(ClassLeader classLeader)
        {
            foreach (object obj in this.classLeaderComboBox.Items)
            {
                ClassLeader currentClassLeader = (ClassLeader)obj;
                if (currentClassLeader.Equals(pupilClass))
                {
                    this.classLeaderComboBox.SelectedItem = currentClassLeader;
                }
            }
        }

        protected void validate(Object sender, EventArgs e)
        {
            bool isValid = !StringUtil.isEmptyString(this.numberBox.Text) &&
                   !StringUtil.isEmptyString(this.literBox.Text);
            this.okButton.Enabled = isValid;
        }


        public Class Class
        {
            get { return pupilClass;  }
        }

        private void okButton_Click_1(object sender, EventArgs e)
        {
            if (pupilClass == null)
            {
                pupilClass = new Class();
            }
            pupilClass.Letter = this.literBox.Text;
            pupilClass.Number = Byte.Parse(this.numberBox.Text);
            pupilClass.ClassLeader = (ClassLeader)this.classLeaderComboBox.SelectedItem;
            this.Close();
        }

        private void cancelButton_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
